package com.jm;


import com.jm.bean.Pl;
import com.jm.tools.RandomTools;
import lombok.extern.slf4j.Slf4j;

import java.util.*;

@Slf4j
public class App {
    public static final List<Pl> pls = new ArrayList<>(10);
    /**
     * 突破10000 为优质客户  并且按排名先后分级
     */
    public static final long defaultOrderNumber = 10000;
    public static long randomScopeScore = 100;

    public static void init() {
        pls.add(new Pl().setId(1L).setVoteScore(10000L).setReserveRank(1).setRandomScopeScore(randomScopeScore));
        pls.add(new Pl().setId(2L).setVoteScore(9877L).setReserveRank(2));
        pls.add(new Pl().setId(3L).setVoteScore(8877L).setReserveRank(3));
        pls.add(new Pl().setId(4L).setVoteScore(7877L).setReserveRank(4));
        pls.add(new Pl().setId(5L).setVoteScore(6877L).setReserveRank(5));
        pls.add(new Pl().setId(6L).setVoteScore(5877L).setReserveRank(6));
        pls.add(new Pl().setId(7L).setVoteScore(4877L).setReserveRank(7));
        pls.add(new Pl().setId(8L).setVoteScore(3877L).setReserveRank(8));
        pls.add(new Pl().setId(9L).setVoteScore(2877L).setReserveRank(9));
        pls.add(new Pl().setId(10L).setVoteScore(1877L).setReserveRank(10));
    }

    public static void main(String[] args) {
        init();
        Timer timer = new Timer();
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                //自增
                auto();
                //计算
                calculate();
                System.out.println("-----------------------------------------------");
                pls.parallelStream().sorted(Comparator.comparing(Pl::getVoteScore).reversed()).forEachOrdered(p -> System.out.println(("id:" + p.getId() + ",分数:" + p.getVoteScore())));

                System.out.println("输入范围");
                String console = input();
                System.out.println(console);
            }
        };


        timer.schedule(timerTask, 1000, 3000);


    }

    public static String input() {
        String in = "";
        Scanner scanner = new Scanner(System.in);
        if (scanner.hasNext()) {
            in = scanner.nextLine();
        }

        scanner.close();
        return in;
    }

    private static void calculate() {
        List<Pl> temps = pls.parallelStream().sorted(Comparator.comparing(Pl::getVoteScore).reversed()).toList();
        for (int i = 0, n = temps.size(); i < n; i++) {
            Pl p = temps.get(i);

            if (i == 0) {
                continue;
            }
            //上一名分数
            long topVoteScore = temps.get(i - 1).getVoteScore();
            //当前分数
            long currentScore = p.getVoteScore();
            //随机范围分数
            p.setRandomScopeScore(topVoteScore - currentScore);
            //预定排名
//            long reserveRank = p.getReserveRank();
            //计算
//            long currentRank = p.getTopRank();

        }

    }

    public static void rank() {

    }

    public static void auto() {
        pls.parallelStream().forEach(i -> {

            int randomScope = Math.toIntExact(Optional.ofNullable(i.getRandomScopeScore()).orElse(0L));
            long random = RandomTools.randomLong(1, randomScope);
            i.setVoteScore(Optional.ofNullable(i.getVoteScore()).orElse(0L) + random);
        });
    }
}
